import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware

class PrepareService implements ApplicationContextAware {
  def servletContext

  void setApplicationContext(ApplicationContext context) { this.servletContext = context.getServletContext() }

  boolean transactional = true

  def say() {
    println "AppService say hello"
  }

  def initCountry() {
    if (Country.count() == 0) {
      servletContext.getResourceAsStream("/res/country.txt").eachLine {line ->
        println line
        def fields = line.split(" 	")
        if (fields.size() == 5)
          new Country(enName: fields[0], chName: fields[1], phoneCode: fields[3]).save()
        else
          new Country(enName: fields[0], chName: fields[1], phoneCode: fields[2]).save()
      }
    }
  }

  def initCity() {
    if (City.count() == 0) {
      def province = null
      servletContext.getResourceAsStream("/res/city.txt").eachLine {line ->
        println line
        def fields = line.split(" ")
        if (fields.size() == 2)
          province = fields[0]
        else if (fields.size() == 3)
          new City(chName: fields[0], enName: fields[1], countryCode: '86', cityCode: fields[2], provinceName: province).save()
        else
          println "ERROR:" + line
      }
    }
  }

  def initCategory() {
    if(Catalog.count() > 0) return
    def knowledge = new Catalog(name: 'knowledge', description: 'knowledge')
    knowledge.save()
    def expert = new Catalog(name: 'expert', description: 'expert', parent: knowledge)
    expert.save()
    def article = new Catalog(name: 'article', description: 'article', parent: knowledge)
    article.save()
    def caseCate = new Catalog(name: 'case', description: 'case')
    caseCate.save()
    def news = new Catalog(name: 'news', description: 'news')
    news.save()
    def lessons = new Catalog(name: 'lessons', description: 'lessons')
    lessons.save()
    def milestone = new Catalog(name: 'milestone', description: 'milestone')
    milestone.save()
    def capability = new Catalog(name: 'capability', description: 'capability', parent:milestone)
    capability.save()
    def development = new Catalog(name: 'development', description: 'development', parent:milestone)
    development.save()
  }

  def initCapability() {
    def capability = Catalog.findByName("capability")
    if (capability) {
      def count = Entry.countByCatalog(capability)
      if(count > 0) return
      boolean newOne = true;
      def title = null
      def content = new StringBuffer()
      def index = 0
      servletContext.getResourceAsStream("/res/milestone.txt").eachLine {line ->
        if (line.trim().size() != 0) {
          if (newOne) {
            title = line
            newOne = false
          } else {
            content << line << "<br/>"
          }
        } else {
          newOne = true
          if (title && content.size() > 0) {
            def entry = new Entry(title: title, content: content.toString(), catalog: capability)
            entry.stage = 20000 + (index*30)
            entry.save()
            if (entry.errors) println entry.errors
            title = null
            content = new StringBuffer()
          }
        }
      }
    }
  }

  def initPlan() {
    def count = Plan.count()
    if (count == 0) {
      servletContext.getResourceAsStream("/res/plan.csv").eachLine {line ->
        def fields = line.split(',')
        if (fields.size() > 2) {
          String name = fields[0]
          Integer start = new Integer(fields[1])
          Integer end = new Integer(fields[2])
          String description = null
          Integer planType = 0
          if (fields.size() == 4)
            description = fields[3]
          if (fields.size() == 5)
            planType = fields[4].toInteger()
          new Plan(name: name, startWith: start, endWith: end, description: description, planType: planType).save()
        }
      }
    }
  }

  def initPersonAndBaby() {
    if (Person.count() > 0) {
      return
    }
    def person = new Person(
            username: 'liguoqiang',
            userRealName: 'liguoqiang',
            passwd: '123',
            enabled: true,
            stage: new Integer(20015),
            email: 'serviceany@gmail.com',
            emailShow: true,
    )

    person.save()
    println person.errors
    def baby = new Baby(
            nickName: 'qiaoqiao',
            realName: 'lihuixin',
            birth: (new Date()) - 15,
            gender: Boolean.TRUE,
            person: person,
    )

    baby.save()
    println baby.errors

  }
}
